<template>
  <view class="report-container">
    <view class="form-card">
      <view class="form-item">
        <text class="label">维护费用（元）</text>
        <input class="input" type="number" v-model="formData.cost" placeholder="请输入费用" />
      </view>

      <view class="form-item">
        <text class="label">维护描述</text>
        <textarea class="textarea" v-model="formData.description" placeholder="请输入详细描述" />
      </view>

      <view class="form-item">
        <text class="label">维护工时（小时）</text>
        <input class="input" type="number" v-model="formData.laborHours" placeholder="请输入工时" />
      </view>

      <view class="form-item">
        <text class="label">更换部件</text>
        <input class="input" v-model="formData.partsReplaced" placeholder="请输入更换部件，多个用逗号分隔" />
      </view>

      <view class="form-item">
        <text class="label">维护日期</text>
        <picker mode="date" @change="dateChange">
          <view class="input">{{ formData.maintenanceDate || '请选择日期' }}</view>
        </picker>
      </view>

      <button class="submit-button" @click="submitReport">提交报告</button>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      formData: {
        id: null,
        cost: null,
        description: '',
        laborHours: null,
        partsReplaced: '',
        maintenanceDate: ''
      }
    };
  },
  onLoad(options) {
    this.formData.id = options.id;
  },
  methods: {
    dateChange(e) {
      this.formData.maintenanceDate = e.detail.value;
    },
    async submitReport() {
      if (!this.validateForm()) return;
      
      uni.showLoading({ title: '提交中...' });
      
      try {
        const res = await uni.request({
          url: 'http://localhost:1000/mesEquipment/updateEqu',
          method: 'POST',
          data: this.formData,
          header: {
            'Content-Type': 'application/json'
          }
        });

        if (res.statusCode === 200 && res.data.code === 200) {
          uni.showToast({ title: '提交成功' });
          setTimeout(() => {
            uni.navigateBack();
          }, 1500);
        } else {
          throw new Error(res.data.msg || '提交失败');
        }
      } catch (error) {
        uni.showToast({
          title: error.message,
          icon: 'none'
        });
      } finally {
        uni.hideLoading();
      }
    },
    validateForm() {
      if (!this.formData.cost) {
        uni.showToast({ title: '请输入费用', icon: 'none' });
        return false;
      }
      if (!this.formData.description) {
        uni.showToast({ title: '请输入描述', icon: 'none' });
        return false;
      }
      return true;
    }
  }
};
</script>

<style  scoped>
.report-container {
  padding: 30rpx;
  background: #f8f9fa;
  min-height: 100vh;
}

.form-card {
  background: #fff;
  border-radius: 16rpx;
  padding: 30rpx;
  box-shadow: 0 4rpx 12rpx rgba(0,0,0,0.1);
}

.form-item {
  margin-bottom: 40rpx;
}

.label {
  display: block;
  font-size: 28rpx;
  color: #333;
  margin-bottom: 16rpx;
}

.input, .textarea {
  width: 100%;
  height: 80rpx;
  padding: 0 20rpx;
  border: 1rpx solid #eee;
  border-radius: 8rpx;
  font-size: 28rpx;
}

.textarea {
  height: 200rpx;
  padding: 20rpx;
}

.submit-button {
  margin-top: 60rpx;
  background: #4080ff;
  color: #fff;
  border-radius: 8rpx;
  font-size: 32rpx;
}
</style>